package com.lf.connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

public class Oracle implements Connect {

    private static final Logger LOGGER = Logger.getLogger(Oracle.class);
    private DataMap dataMap;

    private Connection conn = null;
    private Statement stmt = null;
    private boolean ready = false;
    private PreparedStatement ps = null;
    ResultSet rs = null;

    public Oracle() {
    }

    public Oracle(boolean t) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (Exception e) {
            LOGGER.error(e.getMessage() + "驱动加载失败！");
        }
        try {
            conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@218.6.200.244:1521:aaadb", "aaa",
                    "123$%^");
            stmt = conn.createStatement();
        } catch (SQLException e) {
            LOGGER.error("数据连接失败！" + e.getMessage());
        }
    }

    public void init() {
        try {
            Context context = new InitialContext();
            // 获取连接池对象
            DataSource ds = (DataSource) context
                    .lookup("java:comp/env/jdbcPools");
            // 创建连接
            conn = ds.getConnection();
            stmt = conn.createStatement();
            ready = true;
        } catch (Exception e) {
            LOGGER.error("连接池连接失败！\n" + e.getMessage());
        }
    }

    public void setDataMap(DataMap dataMap) {
        this.dataMap = dataMap;
    }

    public boolean isReady() {
        return ready;
    }

    public void close() {
        try {
            if (rs != null && !rs.isClosed())
                rs.close();
            if (ps != null)
                ps.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            // e.printStackTrace();
            LOGGER.error("连接池关闭异常！\n" + e.getMessage());
        }
    }

    public ResultSet Query(String sql) {
        LOGGER.info(sql);
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            // e.printStackTrace();
            LOGGER.error("数据查询失败：" + e.getMessage());
        }
        return rs;
    }

    @SuppressWarnings("rawtypes")
    public List Query(String sql, Class objclass) {
        ResultSet rs = Query(sql);
        List list = dataMap.Mapping(rs, objclass);
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            LOGGER.error("关闭数据异常：" + e.getMessage());
        }
        return list;
    }

    public int Update(String sql) {
        int rs = 0;
        try {
            rs = stmt.executeUpdate(sql);
        } catch (SQLException e) {
            // e.printStackTrace();
            LOGGER.error("数据修改失败！\n" + e.getMessage());
        }
        return rs;
    }

    public void PS(String sql) {
        LOGGER.info(sql);
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e) {
            // e.printStackTrace();
            LOGGER.error("prepareStatement创建失败！\n" + e.getMessage());
        }
    }

    public void setString(int index, String str) {
        LOGGER.info("set " + index + " : " + str);
        try {
            ps.setString(index, str);
        } catch (SQLException e) {
            // e.printStackTrace();
            LOGGER.error("prepareStatement操作异常！\n" + e.getMessage());
        }
    }

    public void setInt(int index, int data) {
        try {
            ps.setInt(index, data);
        } catch (SQLException e) {
            // e.printStackTrace();
            LOGGER.error("prepareStatement操作异常！\n" + e.getMessage());
        }
    }

    public ResultSet PSQuery() {
        ResultSet rs = null;
        try {
            rs = ps.executeQuery();
        } catch (SQLException e) {
            // e.printStackTrace();
            LOGGER.error("prepareStatement查询异常！\n" + e.getMessage());
        }
        return rs;
    }

    public int PSUpdate() {
        int rs = 0;
        try {
            rs = ps.executeUpdate();
        } catch (SQLException e) {
            // e.printStackTrace();
            LOGGER.error("prepareStatement修改异常！\n" + e.getMessage());
        }
        return rs;
    }

    public static Object getOne(String sql) {
        Oracle con = new Oracle();
        ResultSet rs = con.Query(sql);
        Object temp = null;
        try {
            if (rs != null && rs.next())
                temp = rs.getObject(1);
            rs.close();
            con.close();
        } catch (SQLException e) {
            con.close();
            LOGGER.error("单数据获取异常：" + e.getMessage());
        }
        return temp;
    }

    public static int UpdateOne(String sql) {

        Oracle con = new Oracle();
        int rs = con.Update(sql);
        con.close();
        return rs;
    }
}
